package com.assistant.market.modules.market.domain.gateway.impl;

import com.assistant.framework.BaseRepository;
import com.assistant.market.modules.market.domain.MarketCode;
import com.assistant.market.modules.market.domain.MarketCodeType;
import com.assistant.market.modules.market.domain.QMarketCode;
import com.assistant.market.modules.market.domain.gateway.MarketCodeGatewayCustom;
import com.assistant.util.StringUtils;
import com.querydsl.jpa.impl.JPAQuery;

import java.util.List;

public class MarketCodeGatewayCustomImpl extends BaseRepository implements MarketCodeGatewayCustom {

    @Override
    public List<MarketCode> findAllByTypeAndKeyword(MarketCodeType type, String keyword) {
        final QMarketCode marketCode = QMarketCode.marketCode;
        final JPAQuery<MarketCode> query = queryFactory.selectFrom(marketCode);
        if (type != null) {
            query.where(marketCode.type.eq(type));
        }
        if (StringUtils.hasLength(keyword)) {
            query.where(marketCode.title.contains(keyword)
                    .or(marketCode.remarks.contains(keyword))
                    .or(marketCode.content.contains(keyword))
                    .or(marketCode.author.contains(keyword))
                    .or(marketCode.comments.contains(keyword)));
        }
        return query.fetch();
    }
}
